<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Mapper\ChCorpBlackMapper;
use Carbon\Carbon;
use Mine\Abstracts\AbstractService;
use Mine\MineModel;
use function Hyperf\Collection\data_get;

/**
 * 企业黑名单
 */
class ChCorpBlackService extends AbstractService
{
    public $mapper;

    public function __construct(ChCorpBlackMapper $mapper)
    {
        $this->mapper = $mapper;
    }


    public function list(array $data): array
    {
        $pageSize = data_get($data,'page_size',MineModel::PAGE_SIZE);
        $keyword = data_get($data,'keyword');

        $list = $this->mapper->model::query()
            ->select(['id','corp_name','created_at'])
            ->where('main_uid',$data['main_uid'])
            ->when(!empty($keyword),function ($query) use ($keyword){
                $query->where('corp_name','like','%'.$keyword.'%');
            })
            ->orderBy('created_at','desc')
            ->paginate((int)$pageSize);

        return $this->mapper->setPaginate($list);
    }


    public function save(array $data): mixed
    {
        $data['created_at'] = Carbon::now()->toDateTimeString();
        return $this->mapper->save($data);
    }

    /**
     * 删除企业黑名单
     * @param array $data
     * @return bool|int|mixed|null
     * @throws \Exception
     */
    public function deleteBlack(array $data): mixed
    {
        return $this->mapper->model::query()
            ->where('id',$data['id'])
            ->when('main_uid',$data['main_uid'])
            ->delete();
    }
}